package com.yanqu.road.web.mgr;


import com.yanqu.road.dao.impl.admin.AdminInnerBuyOrderDataDaoImpl;
import com.yanqu.road.entity.admin.AdminInnerBuyOrderData;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AdminMgr extends TempMgr {

    private static Map<String, AdminInnerBuyOrderData> innerBuyOrderDataMap;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        innerBuyOrderDataMap = new AdminInnerBuyOrderDataDaoImpl().getLastAdminInnerBuyOrderData();
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        List<String> removeList = new ArrayList<>();
        long lastTime = DateHelper.getTodayZeroTimeStamp() - DateHelper.DAY_MILLIONS * 7;
        for (Map.Entry<String, AdminInnerBuyOrderData> entry : innerBuyOrderDataMap.entrySet()) {
            if(entry.getValue().getOrderTime() < lastTime){
                removeList.add(entry.getKey());
            }
            if(entry.getValue().isInsertOption()){
                new AdminInnerBuyOrderDataDaoImpl().add(entry.getValue());
            }
        }
        for (String s : removeList) {
            innerBuyOrderDataMap.remove(s);
        }
        return true;
    }

    public static Map<String, AdminInnerBuyOrderData> getInnerBuyOrderDataMap() {
        return innerBuyOrderDataMap;
    }
}
